package com.xyy.saas.payment.adpater.chinabank;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.payment.adpater.chinabank.constants.ChinaBankContants;
import com.xyy.saas.payment.adpater.pingxx.domain.PayDO;
import com.xyy.saas.payment.adpater.pingxx.domain.RefundDO;
import com.xyy.saas.payment.callback.core.handler.CastRequestHandler;
import com.xyy.saas.payment.common.Constants;
import com.xyy.saas.payment.util.DateUtil;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangtianqi
 * @date 2020/7/7 16:08
 */
@Component
@Deprecated
public class CastChinaBankRequestHandler implements CastRequestHandler {

    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(CastChinaBankRequestHandler.class);

    @Override
    public String getChannel() {
        return Constants.CHANNEL_CHINA_BANK_NAME;
    }

    @Override
    public Object castRequestHandler(String method, Map<String, String> request, HttpServletRequest requestMsg) throws Exception {
        Object domain = null;
        switch (method) {
            case Constants.METHOD_PAY_SUCCESS_NAME :
                domain = castToPayDo(requestMsg);
                break;
            case Constants.METHOD_REFUND_SUCCESS :
                domain = castToRefundDO(requestMsg);
                break;
            default:
                LOGGER.error("CastChinaBankRequestHandler#castRequestHandler unrecognized method:{} request:{}", method, JSON.toJSONString(request));
                break;
        }
        return domain;
    }

    private PayDO castToPayDo(HttpServletRequest requestMsg) {
        Map<String, String> params = new HashMap<>();
        Map<String, String[]> requestParams = requestMsg.getParameterMap();
        requestParams.forEach((key, values) -> {
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            params.put(key, valueStr);
        });

        //商户订单号
        String payNo = params.get("outTransNo");
        //支付宝交易号
        String tradeNo = params.get("transNo");
        Integer amount = Integer.parseInt(params.get("transAmount"));
        String resultCode = params.get("resultCode");
        String channelTransactionNo = params.get("bankTradeNo");
        PayDO domain = new PayDO();
        domain.setOrderNo(payNo);
        domain.setChannelChargeNo(tradeNo);
        domain.setAmount(amount);
        String productId = params.get("productId");
        domain.setChannel(Constants.CHANNEL_CHINA_BANK_NAME+"_"+getChannel(productId));
        domain.setResultCode("SUCCESS");
        domain.setChannelTransactionNo(channelTransactionNo);
        return domain;
    }

    private String getChannel(String productId) {
        if (productId.equals(ChinaBankContants.CHANNEL_ALI_NAME)){
            return Constants.CHANNEL_ALI_NAME;
        }
        if (productId.equals(ChinaBankContants.CHANNEL_WX_NAME)){
            return Constants.CHANNEL_WX_NAME;
        }
        return "";
    }
    /**
     * 退款对象
     * @param requestMsg
     * @return
     */
    private RefundDO castToRefundDO(HttpServletRequest requestMsg) {
        RefundDO domain = new RefundDO();
        try {
            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = requestMsg.getParameterMap();
            requestParams.forEach((key, values) -> {
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                params.put(key, valueStr);
            });
            //商户订单号
            String refundNo = params.get("outTransNo");
            String mchNo = params.get("mchNo");
            domain.setRefundNo(refundNo);
            String returnCode = params.get("returnCode");
            String returnMsg = params.get("returnMsg");
            String resultCode = params.get("resultCode");
            String resultMsg = params.get("resultMsg");
            domain.setSucceed(true);
            if(!"0000".equals(returnCode)||!"0000".equals(resultCode)){
                domain.setSucceed(false);
            }
            //第三方退款单号
            domain.setId(params.get("transNo"));
            domain.setTimeSucceed(DateUtil.parase(params.get("payTime"),DateUtil.YYYYMMDDHHMMSS));
        } catch (Exception e){
            LOGGER.error("CastChinaBankRequestHandler.castToRefundDO error：",e);
        }
        return domain;
    }

}
